define([], function() {
    return {
        adminInit: function(_admin) {
            adminObj = {
                search: function(obj) {
                    var param = {};
                    $.each($("#fm-admin").serializeArray(), function(k, v) {
                        var key = $.trim(v.name);
                        var value = $.trim(v.value);
                        param[key] = value;
                    });
                    $('#adminTable').datagrid('load', param);
                },
                reset: function(obj) {
                    $('#fm-admin').form('reset');
                },
                add: function(obj) {
                    $('#dlg-admin-edit').dialog('close');
                    $('#dlg-admin-add').dialog('open').dialog('center').dialog('setTitle', '新增管理员');
                    $('#fm-admin-add').form('clear');
                },
                edit: function(obj) {
                    var json = $(obj).attr('data');
                    var admin = $.parseJSON(json);
                    var title = '编辑管理员(' + admin.name + ')';
                    $('#fm-admin-edit').form('clear');
                    $('#dlg-admin-add').dialog('close');
                    $('#dlg-admin-edit').dialog({
                        onOpen: function() {
                            var body = $(this).panel('body');
                            $.each(admin, function(k, v) {
                                body.find('#admin-edit-' + k).textbox({
                                    value: v
                                }).attr("placeholder", $(this).attr("placeholder"));
                            });
                        }
                    });
                    $('#dlg-admin-edit').dialog('open').dialog('center').dialog('setTitle', title);
                },
                save: function(obj) {
                    var url = _admin.get_add_url;
                    $('#fm-admin-add').form('submit', {
                        url: url,
                        onSubmit: function(param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $.messager.progress('close');
                            }
                            return isValid;
                        },
                        success: function(data) {
                            var _data = $.parseJSON(data);
                            if (_data['status']) {
                                alert(_data['msg']);
                                return false;
                            }
                            $('#dlg-admin-add').dialog('close');
                            $("#adminTable").datagrid('reload');
                        }
                    });
                },
                modify: function(obj) {
                    var url = _admin.get_edit_url;
                    $('#fm-admin-edit').form('submit', {
                        url: url,
                        onSubmit: function(param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $.messager.progress('close');
                            }
                            return isValid;
                        },
                        success: function(data) {
                            var _data = $.parseJSON(data);
                            if (_data['status']) {
                                alert(_data['msg']);
                                return false;
                            }
                            $('#dlg-admin-edit').dialog('close');
                            $("#adminTable").datagrid('reload');
                        }
                    });
                },
                disable: function(obj) {
                    var json = $(obj).attr('data');
                    var admin = $.parseJSON(json);
                    $.messager.confirm('提示', '确定要隐藏管理员-<' + admin['name'] + '>', function(r) {
                        if (r) {
                            var url = _admin.get_edit_url;
                            var data = {
                                'id': admin['id'],
                                'name': admin['name'],
                                'account': admin['account'],
                                'mobile': admin['mobile'],
                                'remarks': admin['remarks'],
                                'status': '0',
                            };
                            //
                            $.ajax({
                                url: url,
                                data: data,
                                success: function(json) {
                                    if (json['status']) {
                                        $.messager.alert('error', json['msg'], 'error');
                                        alert(_data['msg']);
                                        return false;
                                    }
                                    $("#adminTable").datagrid('reload');
                                }
                            });
                        }
                    });
                },
                enable: function(obj) {
                    var json = $(obj).attr('data');
                    var admin = $.parseJSON(json);
                    $.messager.confirm('提示', '确定要启用管理员-<' + admin['name'] + '>', function(r) {
                        if (r) {
                            var url = _admin.get_edit_url;
                            var data = {
                                'id': admin['id'],
                                'name': admin['name'],
                                'account': admin['account'],
                                'mobile': admin['mobile'],
                                'remarks': admin['remarks'],
                                'status': '1',
                            };
                            //
                            $.ajax({
                                url: url,
                                data: data,
                                success: function(json) {
                                    if (json['status']) {
                                        $.messager.alert('error', json['msg'], 'error');
                                        alert(_data['msg']);
                                        return false;
                                    }
                                    $("#adminTable").datagrid('reload');
                                }
                            });
                        }
                    });
                },
                cancel: function(obj) {
                    $('#dlg-admin-add').dialog('close');
                    $('#dlg-admin-edit').dialog('close');
                }
            };
            $("#adminTable").datagrid({
                url: _admin.get_list_url,
                method: 'GET',
                queryParams: {
                    type: 'ajax',
                    admin_id: $("").val(),
                },
                striped: false,
                fit: true,
                border: false,
                fitColumns: true,
                singleSelect: true,
                stripedrd: true,
                rownumbers: true,
                pagination: true,
                idField: 'id',
                nowrap: false,
                pageSize: 35,
                pageList: [35, 40, 50, 100],
                showFooter: true,
                sortName: 'id',
                sortOrder: 'ASC',
                remoteSort: true,
                scrollbarSize: 0,
                columns: [
                    [{
                        field: 'id',
                        checkbox: true,
                    }, {
                        field: 'name',
                        title: '名称',
                        width: '8%',
                        align: 'left'
                    }, {
                        field: 'account',
                        title: '账号',
                        width: '12%',
                        align: 'left'
                    }, {
                        field: 'mobile',
                        title: '电话',
                        width: '15%',
                        align: 'left'
                    }, {
                        field: 'status',
                        title: '状态',
                        width: '5%',
                        align: 'center',
                        sortable: true,
                        formatter: function(value, data, index) {
                            var str = '';
                            if (value == 1) str = '<a style="background:#5cb85c;display:block;color:white;">正常</a>';
                            if (value == 0) str = '<a style="background:#f0ad4e;display:block;color:white;">隐藏</a>';
                            if (value == 3) str = '<a style="background:#d9534f;display:block;color:white;">软删</a>';
                            return str;
                        }
                    }, {
                        field: 'operator_name',
                        title: '最后操作',
                        width: '10%',
                        align: 'center'
                    }, {
                        field: 'updated_at',
                        title: '修改时间',
                        width: '14%',
                        align: 'center',
                        sortable: true
                    }, {
                        field: 'remarks',
                        title: '备注',
                        width: '14%',
                        align: 'center',
                        sortable: false
                    }, {
                        field: 'deleted_at',
                        title: '操作',
                        width: '20%',
                        align: 'center',
                        sortable: false,
                        formatter: function(value, data, index) {
                            var str = '';
                            str += "<a class='loan_admin_edit_button' data='" + JSON.stringify(data) + "' onclick='adminObj.edit(this);'></a>";
                            if (data['status'] == 0) {
                                str += "<a data='" + JSON.stringify(data) + "' onClick='adminObj.enable(this);' class='loan_admin_enable_button'></a>";
                            } else {
                                str += "<a data='" + JSON.stringify(data) + "' onClick='adminObj.disable(this);' class='loan_admin_disable_button'></a>";
                            }
                            // str += "<a class='loan_admin_retset_pwd_button' data='" + JSON.stringify(data) + "' onclick='adminObj.resetPwd(this);'></a>";
                            return str;
                        }
                    }]
                ],
                toolbar: '#adminToolBar',
                onBeforeLoad: function(param) {},
                onLoadSuccess: function(data) {
                    $(".loan_admin_edit_button").linkbutton({
                        text: '编辑',
                        plain: true,
                        iconCls: 'icon-user_edit'
                    });
                    $(".loan_admin_disable_button").linkbutton({
                        text: '隐藏',
                        plain: true,
                        iconCls: 'icon-user_delete'
                    });
                    $(".loan_admin_enable_button").linkbutton({
                        text: '启用',
                        plain: true,
                        iconCls: 'icon-user_tick'
                    });
                    $(".loan_admin_retset_pwd_button").linkbutton({
                        text: '重置',
                        plain: true,
                        iconCls: 'icon-key'
                    });
                    $('#adminTable').datagrid('fixRowHeight');
                },
                onLoadError: function() {},
                onClickCell: function(rowIndex, field, value) {},
                onDblClickRow: function(rowIndex, rowData) {},
                onAfterEdit: function(rowIndex, rowData, changes) {}
            });
        }
    }
});